package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.impl;

import com.css.fxfzxczh.modules.actionTrack.entity.ActionTrackEntity;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.CasualtyInvestigation;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.CasualtyInvestigationRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/17
 * @Description: 人员伤亡调查表
 */
@Repository
public class CasualtyInvestigationRepositoryImpl implements CasualtyInvestigationRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "xczh_casualty_investigation"; // 表名

    @Override
    public void deleteByXlrmId(String xlrmId) {
        String sql = "delete from  "+TABLE_NAME+"  where xlrm_id= '"+xlrmId+"'" ;
        jdbcTemplate.update(sql);
    }

    @Override
    public void batchSave(List<CasualtyInvestigation> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+TABLE_NAME +
                " ( id,xlrm_id,fill_form_user,reviewer,investigation_time,province," +
                "city,county,township,village,seismic_intensity,name,gender,age," +
                "death,missing,badly_injured,minor_injury,cause_of_death,sequence)" +
                "values (:id,:xlrmId,:fillFormUser,:reviewer,:investigationTime,:province," +
                ":city,:county,:township,:village,:seismicIntensity,:name,:gender,:age," +
                ":death,:missing,:badlyInjured,:minorInjury,:causeOfDeath,:sequence)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public List<CasualtyInvestigation> getCasualtyInvestigationByXsemId(String xsemId,String reportType) {
        String sql = "select t1.* from xczh_casualty_investigation t1 left join xczh_loss_report_manage t2 on t1.xlrm_id = t2.id " +
                "where xsem_id = '"+xsemId+"' and  t2.report_type='"+reportType+"' and sequence='合计'";
        List<CasualtyInvestigation> list = namedParameterJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CasualtyInvestigation.class));
        return list;
    }

    @Override
    public List<CasualtyInvestigation> findByXlrmId(String xlrmId) {
        String sql="select name,cause_of_death from xczh_casualty_investigation where xlrm_id= ? and sequence !='合计' and cause_of_death !=''";
        List<CasualtyInvestigation> actionTrack = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CasualtyInvestigation.class),xlrmId);
        return actionTrack;
    }
}
